package com.sunnada.edu.system.pub.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.Utils;

public class BasicController {

	public String msg = null; 
	
	/**
	 * 输出信息
	 * 
	 * @param response
	 * @param obj
	 * @throws IOException
	 */
	protected void printInfo(HttpServletResponse response, Object obj) {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print(obj==null?"":obj);  
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != out){
				out.flush();
				out.close();
			}
		}
	}
	
	
	
	
	protected void setReqParams(HttpServletRequest request,
			Class<?> clazz, Map<String, String> params) {
		if(params!=null){
			params.clear();
		}
		try {
			Enumeration enr = request.getParameterNames();
			while (enr.hasMoreElements()) {
				String key = String.valueOf(enr.nextElement());
				params.put(key, request.getParameter(key));
			}		
			//用户id
			params.put("loginUserId", (String)request.getSession().getAttribute(
					Constants.USER_ID_SESSION));
			params.put("optionUserId", (String)request.getSession().getAttribute(
					Constants.USER_ID_SESSION));
			params.put("optionUserName", (String)request.getSession().getAttribute(
					Constants.USER_NAME_SESSION));
			params.put("authStr", (String)request.getSession().getAttribute(
					"authStr"));

			String selectBegin = null;
			String selectEnd = null;
			if(Constants.DATEBASE_TYPE.equals("oracle")){
				selectBegin = "SELECT * FROM ( SELECT ROWNUM RM,T.* FROM (";
				selectEnd = ") T) WHERE RM>#{sr} AND RM<#{er}";
			}else{
				selectBegin = "SELECT * FROM ( ";
				selectEnd = ") t limit #{sr},#{cr}";
			}
			params.put("selectBegin", selectBegin);
			params.put("selectEnd", selectEnd);
		} catch (Exception e) {
			
		}
	}
	
}
